/*
 * Javasript based on Functions(JBF)
 * 
 * Copyright(c) 2010, Fooxee (JA86.CN)
 *
 * JBF is licensed under the terms of the Open Source GPL 3.0(http://www.gnu.org/licenses/gpl.html) license. 
 * 
 * JBF's blog: http://www.cnblogs.com/JBF/
 * 
 * JBF's project: http://jbfproject.googlecode.com/
 *
 * Created by Fooxee. at 2010.03.30  mailTo:hjccjun@163.com
 */﻿
 
 
/*

#summary JBF.Ajax 主文档.

= JBF.Ajax =

对象:JBF.Ajax

文件:base/Ajax.js

描述:Ajax类.

|| [#Public_Config 配置项] || [#Public_Properties 属性] || [#Public_Methods 方法] || [#Public_Events 事件] ||

=== Public Config ===
公开配置


=== Public Properties ===
公开属性



=== Public Methods ===
公开方法


*/

JBF.Abstract={};


JBF.incIf('JBF.JSON');
JBF.Ajax={};
JBF.Ajax.fn = {
	
	//构造方法
	init : function(options)
	{
		this.setOptions(options);
	},
	//private
	setOptions : function(options)
	{
		this.url=		options.url || '';
		this.method= 	options.method || 'GET';
		this.dataType= 	options.dataType || 'auto';
		this.async=		JBF.isBoolean(options.async) ? options.async : true;
		this.cache=		JBF.isBoolean(options.cache) ? options.cache : true;
		this.params=	JBF.isObject(options.params) ? options.params : {};
		
		this.beforeSend=JBF.isFunction(options.beforeSend) ? options.beforeSend : JBF.emptyFn;
		this.callback=	JBF.isFunction(options.callback) ? options.callback : JBF.emptyFn;
		this.error=		JBF.isFunction(options.error) ? options.error:JBF.emptyFn;
		this.complete=	JBF.isFunction(options.complete) ? options.complete : JBF.emptyFn;
		
		if(this.cache)  this.params.timespace= +new Date();
		this.pString=	this.urlEncode(this.params);
		
		if(this.method.toUpperCase()=='GET') this.url=this.urlAppend(this.url,this.pString);
		
		this.xhr=		JBF.createRequest();
		
	},
	
	/*
	*request*():void
	发起Ajax请求.<br>
	*返回*:{void} <br>
	<hr>
	*/
	request : function()
	{
		var me=this,
		pollHandler,
		stateHandler=function()
		{
			try
			{
				if(me.xhr.readyState==4)
				{
					if(me.xhr.status==200)
					{
						var data=me.xhr.responseText || me.xhr.responseXml;
						try
						{
						if(me.dataType.toLowerCase()=='json')
							data=JBF.JSON.decode(data); 
						me.callback.call(me,data,me.xhr);
						}catch(e)
						{
							me.error.call(me,me.xhr,e.message);	
						}
					}
					else
					{
						me.error.call(me,me.xhr,me.xhr.statusText);
					}
					me.complete.call(me,me.xhr);
					clearTimeout(pollHandler);
				}
			}
			catch(e)
			{
				me.error.call(me,me.xhr,e.message);
				me.complete.call(me,me.xhr);
				clearTimeout(pollHandler);
			}
		};
		
		me.xhr.open(this.method,this.url,this.async);
		if(this.pString)
		{
			me.xhr.setRequestHeader("Content-Length", this.pString.length);
			me.xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded;");
		};
		this.beforeSend.call(this,me.xhr);
		
		if(!+"\v1") me.xhr.onreadystatechange=stateHandler;
		try
		{
			me.xhr.send(this.method.toUpperCase()=='GET'?null:this.pString);
		}
		catch(e)
		{
			me.error.call(this,me.xhr,e.message);
		}
		
		if(!!+"\v1")
		{
			me.xhr.onreadystatechange=stateHandler();
			//异步为真时
			if(this.async)
			{
				pollHandler=setTimeout(function()
				{
					stateHandler();
				},0);
			}
		}
	},
	//private
	urlEncode : function(o, pre)
	{
		var empty,
		buf = [],
		e = encodeURIComponent;
		JBF.each(o,function(k,i,list)
		{
			if(!JBF.isEmpty(k)) buf.push('&', e(k), '=', e(list[k]));
		});
		buf.shift();
		return (pre || '') + buf.join('');
	},
	//private
	urlDecode : function(s)
	{
		if(JBF.isEmpty(s))
		{
			return {};	
		}
		var obj = {},
		pairs,
		d = decodeURIComponent;
		var pms=s.split('?');
		if(pms.length>1)
		{
			pms.shift();
			s=pms.join('');
		}
		pairs = s.split('&');
		
		JBF.each(pairs,function(v,i,list)
		{
			pair = v.split('=');
			obj[pair[0]] = pair[1];
		});
		return obj;
	},
	//private
	urlAppend : function(url, s)
	{
		if(!JBF.isEmpty(s))
		{
			return url + (url.indexOf('?') === -1 ? '?' : '&') + s;
		}
		return url;
	}
};
/*

*/
JBF.urlEncode=JBF.Ajax.fn.urlEncode;
JBF.urlDecode=JBF.Ajax.fn.urlDecode;
JBF.urlAppend=JBF.Ajax.fn.urlAppend;
JBF.Ajax=JBF.createClass(JBF.Ajax.fn);
JBF.apply(JBF.Ajax,{
	/*
	
	*/
	post:function(url,params,callback,async,cache)
	{
		return 	new JBF.Ajax({url:url,params:params,callback:callback,method:'POST',dataType:'auto',async:async,cache:cache,error:callback}).request();
	},
	get:function(url,params,callback,async,cache)
	{
		return 	new JBF.Ajax({url:url,params:params,callback:callback,method:'GET',dataType:'auto',async:async,cache:cache,error:callback}).request();
	},
	postJSON:function(url,params,callback,async,cache)
	{
		return 	new JBF.Ajax({url:url,params:params,callback:callback,method:'POST',dataType:'json',async:async,cache:cache,error:callback}).request();
	},
	getJSON:function(url,params,callback,async,cache)
	{
		return 	new JBF.Ajax({url:url,params:params,callback:callback,method:'GET',dataType:'json',async:async,cache:cache,error:callback}).request();
	},
	//get:function(url,params,callback|listeners,async,cache)
	get:function(url,params,callback,async,cache)
	{
		
	}
});









